define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jieziad/ad/index' + location.search,
                    add_url: 'jieziad/ad/add',
                    edit_url: 'jieziad/ad/edit',
                    del_url: 'jieziad/ad/del',
                    multi_url: 'jieziad/ad/multi',
                    import_url: 'jieziad/ad/import',
                    table: 'jieziad',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='ad_position_id']", form).addClass("selectpage").data("source", "jieziad/ad_position/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'ad_position_id', title: __('Ad_position_id'), formatter: function(value, row){return row.position.name;}},
                        // {field: 'media_type', title: __('Media_type')},
                        {field: 'ad_name', title: __('Ad_name'), operate: 'LIKE'},
                        // {field: 'ad_type', title: __('Ad_type'), operate: 'LIKE'},
                        // {field: 'ad_val', title: __('Ad_val'), operate: 'LIKE'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'click_count', title: __('Click_count')},
                        {field: 'enabled_switch', title: __('Enabled_switch'), table: table, formatter: Table.api.formatter.toggle},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        // {field: 'target', title: __('Target')},
                        // {field: 'bgcolor', title: __('Bgcolor'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#c-ad_type').off('change')
            $('#c-ad_type').on("change", function (e) {
                console.log(e)
                var w = e, ad_type = $('#c-ad_type').val(), ad_val = $('#c-ad_val');
                if(e.type == 'change'){
                    console.log('ad_type', ad_type)

                    if(ad_type){
                        $.each($('#c-ad_type_text').data('selectPageObject').data, function (index, item) {
                            if(item.sign == ad_type){
                                w = item
                            }
                        })
                    }else {
                        w = $('#c-ad_type_text').data('selectPageObject').data[0]
                    }
                }
                console.log('new w ', w)
                if(w.controller){
                    ad_val.attr('data-source', w.controller).addClass('selectpage')
                    if(w.show_name){
                        ad_val.attr('data-field', w.show_name)
                    }
                    if(w.show_mid){
                        ad_val.attr('data-primary-key', w.show_mid)
                    }
                }else{
                    ad_val.attr('type', 'text').removeClass('sp_hidden').addClass('form-control')
                    $('#c-ad_val_text').parent().find('.sp_button').remove()
                    $('#c-ad_val_text').selectPageClear();
                    $('#c-ad_val_text').selectPageData([]);
                    $('#c-ad_val_text').remove()
                }
                Form.api.bindevent($("form[role=form]"));
            });

            $("#c-ad_type").data("eClear", function (e) {
                $('#c-ad_val').attr('type', 'text').removeClass('sp_hidden').addClass('form-control')
                $('#c-ad_val_text').parent().find('.sp_button').remove()
                $('#c-ad_val_text').selectPageClear();
                $('#c-ad_val_text').selectPageData([]);
                $('#c-ad_val_text').remove()
                Form.api.bindevent($("form[role=form]"));
            });
            Controller.api.bindevent();
        },
        edit: function () {
            console.log($("#c-ad_type").val());

            $('#c-ad_type').on("change", function (e) {
                console.log(e)
                var w = e, ad_type = $('#c-ad_type').val(), ad_val = $('#c-ad_val');
                if(e.type == 'change'){
                    console.log('ad_type', ad_type)

                    if(ad_type){
                        $.each($('#c-ad_type_text').data('selectPageObject').data, function (index, item) {
                            if(item.sign == ad_type){
                                w = item
                            }
                        })
                    }else {
                        w = $('#c-ad_type_text').data('selectPageObject').data[0]
                    }
                }
                console.log('new w ', w)
                if(w.controller){
                    ad_val.attr('data-source', w.controller).addClass('selectpage')
                    if(w.show_name){
                        ad_val.attr('data-field', w.show_name)
                    }
                    if(w.show_mid){
                        ad_val.attr('data-primary-key', w.show_mid)
                    }
                }else{
                    ad_val.attr('type', 'text').removeClass('sp_hidden').addClass('form-control')
                    $('#c-ad_val_text').parent().find('.sp_button').remove()
                    $('#c-ad_val_text').selectPageClear();
                    $('#c-ad_val_text').selectPageData([]);
                    $('#c-ad_val_text').remove()
                }
                Form.api.bindevent($("form[role=form]"));
            });
            $(document).one("change", '#c-ad_type', function (e) {
                console.log(e)
                var w = e, ad_type = $('#c-ad_type').val(), ad_val = $('#c-ad_val');
                if(e.type == 'change'){
                    console.log('ad_type', ad_type)

                    if(ad_type){
                        $.each($('#c-ad_type_text').data('selectPageObject').data, function (index, item) {
                            if(item.sign == ad_type){
                                w = item
                            }
                        })
                    }else {
                        w = $('#c-ad_type_text').data('selectPageObject').data[0]
                    }
                }
                console.log('new w ', w)
                if(w.controller){
                    ad_val.attr('data-source', w.controller).addClass('selectpage')
                    if(w.show_name){
                        ad_val.attr('data-field', w.show_name)
                    }
                    if(w.show_mid){
                        ad_val.attr('data-primary-key', w.show_mid)
                    }
                }else{
                    ad_val.attr('type', 'text').removeClass('sp_hidden').addClass('form-control')
                    $('#c-ad_val_text').parent().find('.sp_button').remove()
                    $('#c-ad_val_text').selectPageClear();
                    $('#c-ad_val_text').selectPageData([]);
                    $('#c-ad_val_text').remove()
                }
                Form.api.bindevent($("form[role=form]"));
            });

            $("#c-ad_type").data("eClear", function (e) {
                $('#c-ad_val').attr('type', 'text').removeClass('sp_hidden').addClass('form-control')
                $('#c-ad_val_text').parent().find('.sp_button').remove()
                $('#c-ad_val_text').selectPageClear();
                $('#c-ad_val_text').selectPageData([]);
                $('#c-ad_val_text').remove()
                Form.api.bindevent($("form[role=form]"));
            });
            Controller.api.bindevent();
            var ad_type = $('#c-ad_type').val();
            // if(ad_type) {
                console.log('111')
                var si = setInterval(function () {
                    if($('#c-ad_type_text').data('selectPageObject')){
                        clearInterval(si)
                        console.log('interval')
                        $("#c-ad_type").trigger('change', [$('#c-ad_type_text').data('selectPageObject').data[0]]);
                    }
                    // console.log($('#c-ad_type_text').data('selectPageObject').data[0])
                    // $("#c-ad_type").trigger('change', [$('#c-ad_type_text').data('selectPageObject').data[0]])
                }, 1000)
            // }

        },
        api: {
            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});